<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <a download="测试.txt" href="data:text/txt;charset=utf-8,测试下载纯文本">下载</a> Lorem ipsum dolor sit, amet consectetur adipisicing
    elit. Rem tempore ullam ea, quis incidunt porro non nisi ducimus soluta, molestiae earum doloremque, voluptas officiis
    error itaque maxime iste ipsam blanditiis?
    <button onclick="test()">下载CSV</button><br>
    <button type="button" id="download_btn2">下载CSV2</button>
    <script>
        function test() {
            var csv = '姓名,期中成绩,期末成绩\n张三,58,95\n李四,98,74';
            var a = document.createElement('a');
            a.href = 'data:text/txt;charset=utf-8,\ufeff' + encodeURIComponent(csv);
            a.download = '测试.csv';
            a.click(); // 这里偷个懒，直接用click模拟
        }
    </script>
    
</body>

</html>